<?php
exit;
include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['title'],"title","");
	$g_cgival['title'] = string::un_html($g_cgival['title']);
	
	cgi::both($g_cgival['desc'],"desc","");
	$g_cgival['desc'] = string::un_html($g_cgival['desc']);
	
	
	cgi::both($g_cgival['id'],"id","");
	$g_cgival['id'] = intval($g_cgival['id']);
	
	cgi::both($g_cgival['show_type'],"show_type","");
	$g_cgival['show_type'] = string::un_html($g_cgival['show_type']);
	
	cgi::both($g_cgival['type'],"type","add");
	
	cgi::both($g_cgival['body_url'],"body_url","");
	$g_cgival['body_url'] = string::un_html($g_cgival['body_url']);
	
	
	cgi::both($g_cgival['body_attach'],"body_attach","");
	$g_cgival['body_attach'] = intval($g_cgival['body_attach']);
	
	
	
	cgi::both($g_cgival['mblog_topic'],"mblog_topic","");
	$g_cgival['mblog_topic'] = string::un_html($g_cgival['mblog_topic']);
	
	
	cgi::both($g_cgival['topic_id'],"topic_id","");
	$g_cgival['topic_id'] = string::un_html($g_cgival['topic_id']);
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	$id = $g_cgival['id'] ;
	$title = $g_cgival['title'] ;
	$desc = $g_cgival['desc'] ;
	$type = $g_cgival['type'] ;
	$topic_id = $g_cgival['topic_id'] ;
	$mblog_topic = $g_cgival['mblog_topic'];
	list($mblog_show_type,$mblog_topic_id) = explode("|",$topic_id);
	
	$body_attach = $g_cgival['body_attach'] ;
	$body_url = $g_cgival['body_url'] ;
	$show_order = $g_cgival['show_order'] ;
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		
	}
	else
	{
		throw new Exception("未登录！");
	}
	
	if($type == "add")
	{
		if(!$title)
		{
			throw new Exception("标题不能为空！");
		}
		
		if($id)
		{
			$db_album_info = new db_album_info();
			
			$res = $db_album_info->update_album_info_by_id($id,$uid,$title,$desc,$pic1="",$pic2="",$pic3="",$pic4="",$status_count="",$status="",$show_type,$body_url,$body_attach,$id1="",$id2="",$id3="",$id4="",$count_love="",$count_reply="",$count_good="",$level="",$show_order,$mblog_show_type,$mblog_topic_id,$mblog_topic);
			$g_show["data"]["id"] = $id;
		}
		else
		{
			$db_album_info = new db_album_info();
			$id = $db_album_info->insert_album($title,$desc,$uid,$show_type,$body_url,$body_attach,$show_order,$mblog_show_type,$mblog_topic_id,$mblog_topic,$user_detail_info["user_nickname"],$user_detail_info["sex"],$user_detail_info["verified_type"]);
			$g_show["data"]["id"] = $id;
			$res = $id;
			
			$db_user_info = new db_user_info();
			$res = $db_user_info->update_user_count_by_uid($uid,"",1);
		
		
		}
		
	}
	elseif($type == "del")
	{
		if(!$id)
		{
			throw new Exception("专辑id不能为空！");
		}
		
		
		$db_album_info = new db_album_info();
		
		$item = $db_album_info->get_album_item($id);

		if($item["create_uid"] != $uid)
		{
			throw new Exception("无操作权限！");
		}
		
		$res = $db_album_info->update_album_info_by_id($id,$uid,"","","","","","","",0);
		
		$db_user_info = new db_user_info();
		$db_user_info->update_user_count_by_uid($uid,"",-1);
		
	}
	
	if(!$res)
	{
		throw new  Exception("数据库操作失败！");
	}
	
	
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}